package org.mercworks.knight.animation;

import java.util.List;

import org.mercworks.knightgame.model.dvo.Animation;
import org.mercworks.knightgame.model.dvo.KeyFrame;

public class AnimationManager
{
	private int frameIndexPosition;
	private int keyFrameIndexPosition;
	
	private int frameIndexRotation;
	private int keyFrameIndexRotation;
	
	public AnimationManager()
	{
		frameIndexPosition = 0;
		keyFrameIndexPosition = 0;
		
		frameIndexRotation = 0;
		keyFrameIndexRotation = 0;
	}
	
	public void playAnimation(Animatable animatable, int animationIndex)
	{
		Animation animation = animatable.getListAnimations().get(animationIndex);
		
		/* Set PosX step */
		List<KeyFrame> listKeyFramePosX = animation.getAnimationPosX();
		
		if(listKeyFramePosX != null)
		{
			KeyFrame keyFramePosX = listKeyFramePosX.get(keyFrameIndexPosition);
			
			if(keyFramePosX != null)
				setStep(animatable, keyFramePosX, Movement.TRANSLATION, Direction.X);
		}
		
		List<KeyFrame> listKeyFramePosY = animation.getAnimationPosY();
		
		if(listKeyFramePosY != null)
		{
			KeyFrame keyFramePosY = listKeyFramePosY.get(keyFrameIndexPosition);
			
			if(keyFramePosY != null)
				setStep(animatable, keyFramePosY, Movement.TRANSLATION, Direction.Y);
		}
		
		List<KeyFrame> listKeyFramePosZ = animation.getAnimationPosZ();
		
		if(listKeyFramePosZ != null)
		{
			KeyFrame keyFramePosZ = listKeyFramePosZ.get(keyFrameIndexPosition);
			
			if(keyFramePosZ != null)
				setStep(animatable, keyFramePosZ, Movement.TRANSLATION, Direction.Z);
		}
		
		List<KeyFrame> listKeyFrameRotX = animation.getAnimationRotX();
		
		if(listKeyFrameRotX != null)
		{
			KeyFrame keyFrameRotX = listKeyFrameRotX.get(keyFrameIndexRotation);
			
			if(keyFrameRotX != null)
				setStep(animatable, keyFrameRotX, Movement.ROTATION, Direction.X);
		}
		
		List<KeyFrame> listKeyFrameRotY = animation.getAnimationRotY();
		
		if(listKeyFrameRotY != null)
		{
			KeyFrame keyFrameRotY = listKeyFrameRotY.get(keyFrameIndexRotation);
			
			if(keyFrameRotY != null)
				setStep(animatable, keyFrameRotY, Movement.ROTATION, Direction.Y);
		}
		
		List<KeyFrame> listKeyFrameRotZ = animation.getAnimationRotZ();
		
		if(listKeyFrameRotZ != null)
		{
			KeyFrame keyFrameRotZ = listKeyFrameRotZ.get(keyFrameIndexRotation);
			
			if(keyFrameRotZ != null)
				setStep(animatable, keyFrameRotZ, Movement.ROTATION, Direction.Z);
		}
	}
	private void setStep(Animatable animatable, KeyFrame keyFrame, Movement movement, Direction direction)
	{
		if(keyFrame != null)
		{
			
			float step = keyFrame.getStep();
			
			switch(movement)
			{
				case TRANSLATION:
				{
					if(frameIndexPosition < keyFrame.getMaxFrame())
					{
						switch(direction)
						{
							case X:
								animatable.setDx(step);
								break;
							
							case Y:
								animatable.setDy(step);
								break;
								
							case Z:
								animatable.setDz(step);
								break;
						}
					}
					else
					{
						keyFrameIndexPosition++;
						frameIndexPosition = 0;
					}
				}
				break;
				
				case ROTATION:
				{
					if(frameIndexRotation < keyFrame.getMaxFrame())
					{
						switch(direction)
						{
							case X:
								animatable.setRotDx(step);
								break;
								
							case Y:
								animatable.setRotDy(step);
								break;
								
							case Z:
								animatable.setRotDz(step);
								break;
						}
					}
				}
			}
		}
	}
}
